# 项目名称
PROJECT = xml_parser

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
LDFLAGS = 

# 源文件和目标文件
SOURCES = fastxml.c main.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = $(PROJECT)

# 头文件目录（如果需要）
INCLUDES = -I.

# 库文件（如果需要）
LIBS = 

# 默认目标
all: $(TARGET)

# 测试所有版本
test_all: basic minimal simple
	@echo "所有测试完成"

# 基础测试
test_basic: test_basic.o
	$(CC) test_basic.o -o test_basic $(LDFLAGS) $(LIBS)
	@echo "基础测试程序编译完成: test_basic"

# 运行基础测试
basic: test_basic
	@echo "运行基础测试..."
	./test_basic

# 最小化测试
test_minimal: fastxml.o test_minimal.o
	$(CC) fastxml.o test_minimal.o -o test_minimal $(LDFLAGS) $(LIBS)
	@echo "最小化测试程序编译完成: test_minimal"

# 运行最小化测试
minimal: test_minimal
	@echo "运行最小化测试..."
	./test_minimal

# 安全版本测试
test_safe: fastxml.o fastxml_safe.o test_safe.o
	$(CC) fastxml.o fastxml_safe.o test_safe.o -o test_safe $(LDFLAGS) $(LIBS)
	@echo "安全测试程序编译完成: test_safe"

# 运行安全测试
safe: test_safe
	@echo "运行安全测试..."
	./test_safe

# 简单测试程序
test_simple: fastxml.o test_simple.o
	$(CC) fastxml.o test_simple.o -o test_simple $(LDFLAGS) $(LIBS)
	@echo "简单测试程序编译完成: test_simple"

# 编译测试
simple: test_simple
	@echo "运行简单测试..."
	./test_simple

# 主程序目标
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS) $(LIBS)
	@echo "编译完成: $(TARGET)"

# 编译源文件为对象文件
%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 单独编译fastxml库
libfastxml.a: fastxml.o
	ar rcs libfastxml.a fastxml.o
	@echo "静态库编译完成: libfastxml.a"

# 编译调试版本
debug: CFLAGS += -DDEBUG -g3
debug: $(TARGET)

# 编译发布版本
release: CFLAGS += -DNDEBUG -O3
release: $(TARGET)

# 安装（可选）
install: $(TARGET)
	@echo "安装 $(TARGET) 到 /usr/local/bin/"
	sudo cp $(TARGET) /usr/local/bin/
	sudo chmod 755 /usr/local/bin/$(TARGET)

# 卸载（可选）
uninstall:
	@echo "从 /usr/local/bin/ 卸载 $(TARGET)"
	sudo rm -f /usr/local/bin/$(TARGET)

# 运行测试
test: $(TARGET)
	@echo "运行测试..."
	./$(TARGET)
	@echo "测试完成"

# 运行示例
demo: $(TARGET)
	@echo "运行演示程序..."
	./$(TARGET) example.xml

# 内存检查（需要valgrind）
memcheck: $(TARGET)
	@echo "运行内存检查..."
	valgrind --leak-check=full --show-leak-kinds=all ./$(TARGET)

# 代码格式化（需要indent或clang-format）
format:
	@echo "格式化代码..."
	@if command -v clang-format >/dev/null 2>&1; then \
		clang-format -i *.c *.h; \
		echo "使用 clang-format 格式化完成"; \
	elif command -v indent >/dev/null 2>&1; then \
		indent -linux *.c *.h; \
		echo "使用 indent 格式化完成"; \
	else \
		echo "未找到代码格式化工具 (clang-format 或 indent)"; \
	fi

# 静态分析（需要cppcheck）
analyze:
	@echo "运行静态代码分析..."
	@if command -v cppcheck >/dev/null 2>&1; then \
		cppcheck --enable=all --std=c99 *.c *.h; \
	else \
		echo "未找到 cppcheck 工具"; \
	fi

# 生成文档（需要doxygen）
docs:
	@echo "生成文档..."
	@if command -v doxygen >/dev/null 2>&1; then \
		doxygen Doxyfile; \
		echo "文档生成完成，查看 docs/html/index.html"; \
	else \
		echo "未找到 doxygen 工具"; \
	fi

# 清理编译文件
clean:
	@echo "清理编译文件..."
	rm -f $(OBJECTS) $(TARGET) libfastxml.a test_simple test_simple.o test_safe test_safe.o fastxml_safe.o test_minimal test_minimal.o test_basic test_basic.o
	rm -f *.o *~ core
	@echo "清理完成"

# 深度清理
distclean: clean
	rm -rf docs/
	rm -f *.bak *.orig

# 显示帮助信息
help:
	@echo "FastXML 构建系统"
	@echo "================"
	@echo ""
	@echo "可用目标:"
	@echo "  all        - 编译程序 (默认)"
	@echo "  debug      - 编译调试版本"
	@echo "  release    - 编译发布版本"
	@echo "  libfastxml.a - 编译静态库"
	@echo "  test       - 运行测试"
	@echo "  demo       - 运行演示"
	@echo "  install    - 安装程序"
	@echo "  uninstall  - 卸载程序"
	@echo "  memcheck   - 内存检查"
	@echo "  format     - 代码格式化"
	@echo "  analyze    - 静态代码分析"
	@echo "  docs       - 生成文档"
	@echo "  clean      - 清理编译文件"
	@echo "  distclean  - 深度清理"
	@echo "  help       - 显示此帮助信息"
	@echo ""
	@echo "使用示例:"
	@echo "  make           # 编译程序"
	@echo "  make debug     # 编译调试版本"
	@echo "  make test      # 运行测试"
	@echo "  make clean     # 清理文件"

# 声明伪目标
.PHONY: all debug release install uninstall test demo memcheck format analyze docs clean distclean help simple safe minimal basic test_all

# 显示编译信息
info:
	@echo "项目信息:"
	@echo "  项目名称: $(PROJECT)"
	@echo "  编译器:   $(CC)"
	@echo "  编译选项: $(CFLAGS)"
	@echo "  源文件:   $(SOURCES)"
	@echo "  目标文件: $(TARGET)"